package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import uoc.biblioteca.mappings.object.LlibreVo;
import uoc.biblioteca.mappings.object.SolicitudLlibreVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class Llibre.
 * @see uoc.biblioteca.mappings.dao.LlibreVo
 * @author Hibernate Tools
 */
public class LlibreHome {

	private static final Log log = LogFactory.getLog(LlibreHome.class);

	private final SessionFactory sessionFactory = null;


	public void saveNew(LlibreVo instance) {
		
		log.debug("save new LlibreVo instance");
		
		try {
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
		
		
	}

	public LlibreVo findById(java.lang.Integer id) {
		log.debug("getting LlibreVo instance with id: " + id);
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			LlibreVo instance = (LlibreVo) session.get("uoc.biblioteca.mappings.object.LlibreVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(LlibreVo instance) {
		log.debug("finding LlibreVo instance by example");
		try {
			List results = sessionFactory.getCurrentSession()
					.createCriteria("uoc.biblioteca.mappings.object.LlibreVo")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	//Mostrar todos los libros solicitados duplicados por titol 
	public LlibreVo[] getAllLlibreDuplicatedByTitol() {
		
		/* will hold the books we are going to return later */
		try{
			List solicituds = new ArrayList();
			
			/*
			 * SELECT DISTINCT
				`TestCabra`.`Llibre`.`Titol`, `TestCabra`.`Llibre`.`ISBN`
				FROM
				`TestCabra`.`Llibre`
				WHERE
				`TestCabra`.`Llibre`.`Titol`
				In (
				SELECT `TestCabra`.`Llibre`.`Titol` FROM `TestCabra`.`Llibre` As Tmp GROUP BY `TestCabra`.`Llibre`.`Titol` HAVING Count(*) > 1)
				ORDER BY
				`TestCabra`.`Llibre`.`Titol` 
			 */
			/*
			 * SELECT `TestCabra`.`Llibre`.`Titol` FROM `TestCabra`.`Llibre` GROUP BY `TestCabra`.`Llibre`.`Titol` HAVING Count(*) > 1
			 */
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select d from uoc.biblioteca.mappings.object.LlibreVo as d group by d.titol having count(*) > 1 order by d.titol ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				LlibreVo llibreTemp = (LlibreVo) iter.next();
				//solicitudTemp.getLlibre();
				//solicitudTemp.getEstadoSolicitud();
				solicituds.add(llibreTemp);
			}
			tx.commit();
			
			return (LlibreVo[]) solicituds.toArray(new LlibreVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
